{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>03-assert断言函数及用法<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python assert 语句，又称断言语句，可以看做是功能缩小版的 if 语句，它用于判断某个表达式的值，如果值为真，则程序可以继续往下执行；反之，Python 解释器会报 AssertionError 错误。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "assert 语句的语法结构为：\n",
    "`assert 表达式`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "assert 语句的执行流程可以用 if 判断语句表示，如下所示：\n",
    "\n",
    "`\n",
    "if 表达式==True:\n",
    "    程序继续执行\n",
    "else:\n",
    "    程序报 AssertionError 错误\n",
    "`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的分数：20\n",
      "数学考试分数为： 20\n"
     ]
    }
   ],
   "source": [
    "mathmark = int(input('请输入你的分数：'))\n",
    "#断言数学考试分数是否位于正常范围内\n",
    "assert 0 <= mathmark <= 100\n",
    "#只有当 mathmark 位于 [0,100]范围内，程序才会继续执行\n",
    "print(\"数学考试分数为：\",mathmark)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "03-assert断言函数及用法",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
